package com.jinzecloud.blog.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDate;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.crypto.SecureUtil;
import com.jinzecloud.blog.bean.User;
import com.jinzecloud.blog.service.UserService;
import com.jinzecloud.blog.utils.Page;
import com.jinzecloud.blog.utils.Result;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiOperation;
//import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;

/**
 * 用户表
 *
 * @author kumi
 * @email 1971240051@qq.com
 * @date 2022-06-04 03:21:45
 */
//@Api(tags = "用户后端 API 接口")
@RestController
@RequestMapping("user/user")
public class UserController {

    @Autowired
    UserService userService;

//    @ApiOperation(value = "添加用户列表", notes = "没有返回，可以省略主键id，添加时间，更新时间以及状态")
    @RequestMapping("/add")
    public Result addUser(@RequestBody User user){
        try {
            userService.add(user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"添加失败");
        }
        return Result.ok().put("data","添加成功");
    }

    @RequestMapping("/alter")
//    @ApiOperation(value = "修改用户", notes = "没有返回，可以省略状态")
    public Result alterUser(@RequestBody User user){
        try {
            userService.alter(user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"修改失败");
        }
        return Result.ok().put("data","修改成功");
    }

    @RequestMapping("/delete")
//    @ApiOperation(value = "删除用户", notes = "没有返回")
    public Result deleteUser(@RequestBody Long[] id){
        try {
            userService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"删除失败");
        }
        return Result.ok().put("data","删除成功");
    }


    @RequestMapping(value = "/byId/{id}")
//    @ApiOperation(value = "根据id查询用户", notes = "返回用户信息")
    public Result deleteUser(@PathVariable("id") Long id){
        User user = null;
        try {
            user = userService.byId(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"查询失败");
        }
        return Result.ok().put("data",user);
    }

    @RequestMapping(value="/list")
//    @ApiOperation(value = "查询用户列表", notes = "返回用户全列表")
    public Result listUser(@RequestBody Map<String, Object> params){
        Page<User> page = null;
        try {
            page = userService.listUser(params);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"查询失败");
        }
        return Result.ok().put("data",page);
    }

    @RequestMapping("/updateStatus")
//    @ApiOperation(value = "修改用户状态", notes = "可只传用户id和状态")
    public Result updateStatus(@RequestBody User user){
        try {
            userService.updateStatus(user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"用户状态修改失败");
        }
        return Result.ok().put("data","用户状态修改成功");
    }

    @RequestMapping("/updatePassword")
//    @ApiOperation(value = "修改用户密码", notes = "可只传用户id或用户名和密码")
    public Result updatePassword(@RequestBody User user){
        try {
            userService.updatePassword(user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"用户密码修改失败");
        }
        return Result.ok().put("data","用户密码修改成功");
    }
}
